package service

import (
	"fmt"
	"gin/types"
	"strconv"
)

type ProductionStepCard struct {
}

// StepCardList
func (P ProductionStepCard) StepCardList(episodeData []types.EpisodeData, EpisodeRelationStepDataArrayGroupMap, EpisodeTaskNotTimeData map[string]types.StepEpisodeTask,
	MakingDataArrayGroupMap, makingNotTimeData map[string]types.StepMakingList, OverdueNumDataMap, OverdueNumNotTimeData map[string]types.OverDueStepNumRes,
	StepTaskData []types.StepTaskInfoRes, GetStepFinishTaskData map[string]types.StepFinishTask,
	getStepNotCloseWorkData map[string]notCloseWorkOrder, GetStepRepairIngData map[string]types.StepRepairIng, GetStepRepairData map[string]types.StepRepair,
	Req types.ProductionManageInfoReq) (res []interface{}, err error) {

	for _, Item := range episodeData {

		var EpisodeInfoRes = EpisodeInfo{
			Id:      Item.EpisodeId,
			Name:    Item.EntityCode,
			Deliver: "",
			Play:    "",
		}
		var CardInfoLists = make(map[string]interface{})
		var ColumnItemData []ColumnItem
		// StepTaskData
		for _, StepItem := range StepTaskData {
			ColumnItemData = append(ColumnItemData, ColumnItem{
				Name:  StepItem.StepName,
				Code:  StepItem.StepCode,
				Index: StepItem.ShowIndex,
			})
			var TotalNum uint64
			TotalNum = 0
			matchKey := fmt.Sprintf("%d-%d", Item.EpisodeId, StepItem.StepId)
			if _, ok := EpisodeRelationStepDataArrayGroupMap[matchKey]; ok {
				TotalNum = EpisodeRelationStepDataArrayGroupMap[matchKey].Num
			}
			var MakingNum uint64
			MakingNum = 0
			if _, ok := MakingDataArrayGroupMap[matchKey]; ok {
				MakingNum = MakingDataArrayGroupMap[matchKey].MakingNum
			}
			var OverdueNum uint64
			OverdueNum = 0
			if _, ok := OverdueNumDataMap[matchKey]; ok {
				OverdueNum = OverdueNumDataMap[matchKey].OverdueNum
			}
			// 返修率 = 总返修数量 / 总任务
			var RepairRateNum float64
			RepairRateNum = 0
			if _, ok := GetStepRepairData[matchKey]; ok {
				if GetStepRepairData[matchKey].RepairNum > 0 && TotalNum > 0 {
					tmpNum := ((float64(GetStepRepairData[matchKey].RepairNum)) / float64(TotalNum)) * 100
					RepairRateNum, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", tmpNum), 64)
				}
			}
			var EpisodeTaskNotTimeNum uint64
			EpisodeTaskNotTimeNum = 0
			if _, ok := EpisodeTaskNotTimeData[matchKey]; ok {
				EpisodeTaskNotTimeNum = EpisodeTaskNotTimeData[matchKey].Num
			}
			var makingNotTimeNum uint64
			makingNotTimeNum = 0
			if _, ok := makingNotTimeData[matchKey]; ok {
				makingNotTimeNum = makingNotTimeData[matchKey].MakingNum
			}
			var OverdueNumNotTimeNum uint64
			OverdueNumNotTimeNum = 0
			if _, ok := OverdueNumNotTimeData[matchKey]; ok {
				OverdueNumNotTimeNum = OverdueNumNotTimeData[matchKey].OverdueNum
			}
			var stepFinishTaskNum uint64
			stepFinishTaskNum = 0
			if _, ok := GetStepFinishTaskData[matchKey]; ok {
				stepFinishTaskNum = GetStepFinishTaskData[matchKey].Num
			}
			var StepNotCloseWorkNum uint64
			StepNotCloseWorkNum = 0
			if _, ok := getStepNotCloseWorkData[matchKey]; ok {
				StepNotCloseWorkNum = getStepNotCloseWorkData[matchKey].Num
			}
			// 返修中
			var StepRepairIngNum uint64
			StepRepairIngNum = 0
			if _, ok := GetStepRepairIngData[matchKey]; ok {
				StepRepairIngNum = GetStepRepairIngData[matchKey].RepairIngNum
			}

			var Schedule float64
			Schedule = 0
			if TotalNum > 0 { // 计算进度
				Schedule = (float64(stepFinishTaskNum) / float64(TotalNum)) * 100
				Schedule, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", Schedule), 64)
			}
			var FilterTaskStatisticsItem TaskStatistics
			// 周期内-有时间筛选条件
			FilterTaskStatisticsItem = TaskStatistics{
				Total:    TotalNum,
				MakeIng:  MakingNum,
				Overdue:  OverdueNum,
				Schedule: Schedule,
			}
			if Req.StartTime <= 0 && Req.EndTime <= 0 {
				EpisodeTaskNotTimeNum = TotalNum
				makingNotTimeNum = MakingNum
				OverdueNumNotTimeNum = OverdueNum
			}
			var TaskStatisticsItem TaskStatistics
			// 总计-没有时间筛选条件
			TaskStatisticsItem = TaskStatistics{
				Total:    EpisodeTaskNotTimeNum,
				MakeIng:  makingNotTimeNum,
				Overdue:  OverdueNumNotTimeNum,
				Schedule: Schedule,
			}
			CardInfoLists[StepItem.StepCode] = ListItem{
				Code:               StepItem.StepCode,
				RepairIng:          StepRepairIngNum,
				NotClosedWorkOrder: StepNotCloseWorkNum,
				RepairRate:         RepairRateNum,
				TotalTask:          TaskStatisticsItem,
				FilterTask:         FilterTaskStatisticsItem,
				StepCategoryId:     StepItem.StepCategoryId,
				StepId:             StepItem.StepId,
				StepName:           StepItem.StepName,
			}
		}
		var tmpRes = ProductionManageRes{
			EpisodeInfo:  EpisodeInfoRes,
			CardInfoList: CardInfoLists, // 卡片集合
			Column:       ColumnItemData,
		}
		res = append(res, tmpRes)
	}
	return
}
